package com.kc.business.service.impl;

import com.kc.business.domain.*;
import com.kc.business.mapper.*;
import com.kc.business.service.IBizMarketEnterService;
import com.kc.common.constant.BizConstants;
import com.kc.common.constant.UserConstants;
import com.kc.common.core.domain.entity.SysRole;
import com.kc.common.core.domain.entity.SysUser;
import com.kc.common.exception.CustomException;
import com.kc.common.utils.JPushUtil;
import com.kc.common.utils.SecurityUtils;
import com.kc.common.utils.StringUtils;
import com.kc.system.domain.SysUserRole;
import com.kc.system.mapper.SysRoleMapper;
import com.kc.system.mapper.SysUserMapper;
import com.kc.system.mapper.SysUserRoleMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * TODO
 *
 * @author zhangshuo
 * @version 1.0
 * @date 2020/9/25
 */
@Service
public class BizMarketEnterServiceImpl implements IBizMarketEnterService {
    @Resource
    BizMarketEnterMapper marketEnterMapper;
    @Resource
    BizUserMapper userMapper;
    @Resource
    BizMarketMapper marketMapper;
    @Resource
    BizMarketBusinessMapper bizMarketBusinessMapper;
    @Resource
    SysRoleMapper sysRoleMapper;
    @Resource
    BizRoleMapper bizRoleMapper;
    @Resource
    BizUserRoleMapper bizUserRoleMapper;
    @Resource
    SysUserMapper adminMapper;
    @Resource
    SysUserRoleMapper sysUserRoleMapper;

    /**
     * 市场入驻列表
     *
     * @param marketEnterForm
     * @return
     */
    @Override
    public List<BizMarketEnterForm> getList(BizMarketEnterForm marketEnterForm) {
        return marketEnterMapper.select(marketEnterForm);
    }

    /**
     * 市场入驻审核
     *
     * @param marketEnterForm
     * @return
     */
    @Override
    @Transactional
    public int edit(BizMarketEnterForm marketEnterForm) {
        if (BizConstants.PASS_VERIFY.equals(marketEnterForm.getStatus())) {
            BizMarketEnterForm enterForm = marketEnterMapper.selectByPrimaryKey(marketEnterForm.getEnterId());
            int useId = enterForm.getUserId();
            //更新APP用户
            BizUser bizUser = userMapper.selectByPrimaryKey(useId);
            bizUser.setId(useId);
            bizUser.setIsMarket(BizConstants.YES);
            userMapper.updateByPrimaryKeySelective(bizUser);

            //创建商家
            BizMarketBusiness marketBusiness = new BizMarketBusiness();
            marketBusiness.setUserId(useId);
            marketBusiness.setLatitude(enterForm.getLatitude());
            marketBusiness.setLongitude(enterForm.getLongitude());
            marketBusiness.setName(enterForm.getShopName());
            marketBusiness.setMarketId(enterForm.getMarketId());
            marketBusiness.setCreateTime(LocalDateTime.now());
            BizMarket market = null;
            //创建市场
            if (StringUtils.isNotBlank(enterForm.getMarketName())) {
                //新增市场
                market = new BizMarket();
                market.setAddress(enterForm.getShopAddress());
                market.setLatitude(enterForm.getLatitude());
                market.setLongitude(enterForm.getLongitude());
                market.setName(enterForm.getShopName());
                market.setBusinessNum(1);
                market.setAdcode(enterForm.getAdcode());
                marketMapper.insertSelective(market);
                marketBusiness.setMarketId(market.getId());
            } else {
                market = marketMapper.selectByPrimaryKey(marketEnterForm.getMarketId());
                market.setBusinessNum(market.getBusinessNum() + 1);
                marketMapper.updateByPrimaryKeySelective(market);
            }

            bizMarketBusinessMapper.insertSelective(marketBusiness);

            //创建市场PC端账号
            SysUser admin = new SysUser();
            admin.setNickName(enterForm.getName());
            admin.setPhonenumber(bizUser.getMobile());
            admin.setUserName(bizUser.getMobile());
            if (UserConstants.NOT_UNIQUE.equals(adminMapper.checkUserNameUnique(admin.getUserName()) > 0 ? "1" : "0")) {
                admin = adminMapper.selectUserByUserName(admin.getUserName());
            } else {
                //身份证后6位做密码
                String idCard = enterForm.getIdCard();
                String password = idCard.substring(idCard.length() - 6, idCard.length());
                admin.setPassword(SecurityUtils.encryptPassword(password));
                admin.setTenantId(enterForm.getUserId());
                adminMapper.insertUser(admin);
            }
            //查询PC角色
            SysRole sysRole = sysRoleMapper.checkRoleKeyUnique(UserConstants.ROLE_MARKET_PC);
            if (sysRole == null) {
                throw new CustomException("请联系管理员，创建PC角色");
            }
            //设置后台用户角色
            SysUserRole userRole = new SysUserRole();
            userRole.setUserId(admin.getUserId());
            userRole.setRoleId(sysRole.getRoleId());

            // 新增用户与角色管理
            List<SysUserRole> list = new ArrayList<SysUserRole>();
            list.add(userRole);
            sysUserRoleMapper.batchUserRole(list);

            //查询APP角色
            BizRole appRole = bizRoleMapper.getActiveByRoleKey(UserConstants.ROLE_MARKET_APP);
            if (appRole == null) {
                throw new CustomException("请联系管理员，创建APP角色");
            }
            //设置APP用户角色
            BizUserRole bizUserRole = new BizUserRole();
            bizUserRole.setUserId(bizUser.getId());
            bizUserRole.setRoleId(appRole.getId());
            bizUserRoleMapper.insert(bizUserRole);
        }
        return marketEnterMapper.updateByPrimaryKeySelective(marketEnterForm);
    }
}
